package com.third.customer.model.dao.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.third.customer.model.dao.AreaDao;
import com.third.customer.model.dao.param.AreaParam;
import com.third.customer.model.mapper.AreaMapper;
import com.third.customer.model.tables.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by hehuaichun on 2020/5/21.
 */
@Repository
public class AreaDaoImpl implements AreaDao {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Page<Area> query(AreaParam param) {
        Page<Area> page = new Page<>(param.getPage(), param.getLimit());
        page.addOrder(OrderItem.desc("id"));
        return new LambdaQueryChainWrapper<>(areaMapper)
                .eq(Area::getCompanyId, param.getCompanyId())
                .like(!StringUtils.isEmpty(param.getCommunity()), Area::getCommunity, param.getCommunity())
                .like(!StringUtils.isEmpty(param.getCounty()), Area::getCounty, param.getCounty())
                .page(page);
    }

    @Override
    public Optional<Area> getById(long companyId, Long areaId) {
        Area area = new LambdaQueryChainWrapper<>(areaMapper)
                .eq(Area::getId, areaId)
                .eq(Area::getCompanyId, companyId)
                .one();
        return Optional.ofNullable(area);
    }

    @Override
    public List<Area> getByIds(long companyId, List<Long> areaIds) {
        if(CollectionUtils.isEmpty(areaIds)){
            return Lists.newArrayList();
        }
        return new LambdaQueryChainWrapper<>(areaMapper)
                .eq(Area::getCompanyId, companyId)
                .in(Area::getId, areaIds)
                .list();
    }

    @Override
    public List<Area> getByCompanyId(long companyId) {
        return new LambdaQueryChainWrapper<>(areaMapper)
                .eq(Area::getCompanyId, companyId)
                .list();
    }

    @Override
    public Optional<Area> getByAreaName(Long companyId, String county, String community) {
        boolean isNullCounty = StringUtils.isEmpty(county);
        boolean isNullCommunity = StringUtils.isEmpty(community);
        Area area = new LambdaQueryChainWrapper<>(areaMapper).eq(Area::getCompanyId, companyId)
                .eq(!isNullCounty, Area::getCounty, county).isNull(isNullCounty, Area::getCompanyId)
                .eq(!isNullCommunity, Area::getCommunity, community).isNull(isNullCommunity, Area::getCommunity)
                .one();
        return Optional.ofNullable(area);
    }

    @Override
    public boolean insert(Area area) {
        return areaMapper.insert(area) == 1;
    }

    @Override
    public boolean update(Area area) {
        return areaMapper.updateById(area) == 1;
    }

    @Override
    public boolean delete(Long id) {
        return areaMapper.deleteById(id) == 1;
    }
}
